package com.vq3.digitalizationsoftwareintegrates.Service.MessageCenter.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.Group_Type;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.MessageType;
import com.vq3.digitalizationsoftwareintegrates.Entity.MessageCenter.BusinessCenterMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.MessageCenter.BusinessCenterMessageEntity_per;
import com.vq3.digitalizationsoftwareintegrates.Entity.MessageCenter.MessageCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.MessageCenter.MessageCenter_PerEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.*;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.*;
import com.vq3.digitalizationsoftwareintegrates.Mapper.MessageCenter.BusinessCenterMessageMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.MessageCenter.BusinessCenterMessage_perMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.MessageCenter.MessageCenterMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.MessageCenter.MessageCenter_PerMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.Group_TBMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.*;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_SaveDailyContextDao;
import com.vq3.digitalizationsoftwareintegrates.Service.MessageCenter.MessageCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;
import com.vq3.digitalizationsoftwareintegrates.Utils.PermissionControl.GetUserPermission;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class MessageCenterServiceImpl implements MessageCenterService {
    @Resource
    MessageCenterMapper messageCenterMapper;
    @Resource
    MessageCenter_PerMapper messageCenter_perMapper;
    @Resource
    User_RoleMapper user_roleMapper;
    @Resource
    MongoDB_S_C_Point_ContentDao mongoDB_s_c_point_contentDao;
    @Resource
    UserMapper userMapper;
    @Resource
    URole_TBMapper uRole_tbMapper;
    @Resource
    Group_TBMapper group_tbMapper;
    @Resource
    MongoDB_S_C_SaveDailyContextDao mongoDB_s_c_saveDailyContextDao;
    @Resource
    BusinessCenterMessageMapper businessCenterMessageMapper;
    @Resource
    GetUserPermission getUserPermission;
    @Resource
    UserLoginService userLoginService;
    @Resource
    BusinessCenterMessage_perMapper businessCenterMessage_perMapper;


    public List<MessageCenter_PerEntity> uidFindRoleIdFindMid(Permission_TBEntity permission_tbEntity) throws JsonProcessingException {
        QueryWrapper<MessageCenter_PerEntity> perEntityQueryWrapper = new QueryWrapper<>();
        perEntityQueryWrapper.eq("per", permission_tbEntity.getPermission());
        return messageCenter_perMapper.selectList(perEntityQueryWrapper);


    }

    public List<User_RoleEntity> uidFindU_RTBToRoles(int uid) {
        QueryWrapper<User_RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid);
        return user_roleMapper.selectList(queryWrapper);
    }

    public List<MongoDB_S_C_WebSocketMessageEntity> rolesFindMongoDBToMongoDB_S_C_WebSocketMessageEntityS(Permission_TBEntity permission_tbEntity) {
        return mongoDB_s_c_point_contentDao.findMongoDB_S_C_WebSocketMessage(permission_tbEntity);
    }

    public List<MongoDB_S_C_Point_ContentEntity> itemNameFindMongoDBToMongoDB_S_C_Point_ContentEntityS(List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketMessageEntities) {
        return mongoDB_s_c_point_contentDao.itemNameFindMongoDBToMongoDB_S_C_Point_ContentEntityS(mongoDB_s_c_webSocketMessageEntities);
    }

    public List<MongoDB_S_C_WebSocketSendMessageEntity> findNormalMessages(String uname) {
        return mongoDB_s_c_point_contentDao.findNormalMessages(uname);
    }

    public List<MongoDB_S_C_WebSocketSendMessageEntity> findSysMessages(List<User_RoleEntity> user_roleEntities) {
        return mongoDB_s_c_point_contentDao.findSysMessages(user_roleEntities);
    }

    public boolean deleteMessage(String itemName, String uname) {
        return mongoDB_s_c_point_contentDao.deleteMessage(itemName, uname);
    }

    public List<UserEntity> findUserAll() {
        return userMapper.selectList(null);

    }

    public List<URole_TBEntity> findURole() {
        return uRole_tbMapper.selectList(null);
    }

    public void saveSysMessage(MongoDB_S_C_WebSocketMessageEntity messageEntity) {
        mongoDB_s_c_point_contentDao.saveSysMessage(messageEntity);
    }

    public boolean deleteSysMessage(String message, Integer date, Integer month, Integer hour, MessageType messageType) {
        return mongoDB_s_c_point_contentDao.deleteSysMessage(message, date, month, hour, messageType);
    }

    public URole_TBEntity uroleFindURoleTB(String urole) {
        QueryWrapper<URole_TBEntity> uRole_tbEntityQueryWrapper = new QueryWrapper<>();
        uRole_tbEntityQueryWrapper.eq("urole", urole);
        return uRole_tbMapper.selectOne(uRole_tbEntityQueryWrapper);

    }

    public List<MongoDB_S_C_SysAuthAuditMessageEntity> findUserAuthAuditMessage(Group_Type group_type) {
        return mongoDB_s_c_point_contentDao.findUserAuthAuditMessage(group_type);
    }

    public Group_TBEntity groupIdFindGroup(Integer groupId) {
        QueryWrapper<Group_TBEntity> groupTbEntityQueryWrapper = new QueryWrapper<>();
        groupTbEntityQueryWrapper.eq("group_id", groupId);
        return group_tbMapper.selectOne(groupTbEntityQueryWrapper);
    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_DailyAuditMessage(Permission_TBEntity permission_tbEntity) {
        return mongoDB_s_c_saveDailyContextDao.findS_C_DailyAuditMessage(permission_tbEntity);
    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_DailyAuditMessage(Permission_TBEntity permission_TBEntity, Date date, String pointName) {
        return mongoDB_s_c_saveDailyContextDao.findS_C_DailyAuditMessage(permission_TBEntity, date, pointName);
    }

    @Override
    public boolean auditPassBtnFun(String userName) {
        boolean flag = false;
        MongoDB_S_C_SysAuthAuditMessageEntity s_c_sysAuthAuditMessageEntity = mongoDB_s_c_saveDailyContextDao.getUserAudit(userName);
        if (s_c_sysAuthAuditMessageEntity != null) {
            UserEntity toUserEntity = s_c_sysAuthAuditMessageEntity.getToUserEntity();
            String fullname = toUserEntity.getFullname();
            Integer groupId = toUserEntity.getGroupId();

            UpdateWrapper<UserEntity> userEntityUpdateWrapper = new UpdateWrapper<>();
            userEntityUpdateWrapper.eq("uname", userName);
            userEntityUpdateWrapper.set("SysAuthentication", "YES");
            userEntityUpdateWrapper.set("group_id", groupId);
            userEntityUpdateWrapper.set("fullname", fullname);


            int update = userMapper.update(null, userEntityUpdateWrapper);
            if (update > 0) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public boolean auditNGBtnFun(String userName) {
        boolean b = mongoDB_s_c_saveDailyContextDao.auditNGBtnFun(userName);
        if (b) {
            QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
            userEntityQueryWrapper.eq("uname", userName);
            int delete = userMapper.delete(userEntityQueryWrapper);
            return delete > 0;
        }
        return false;
    }

    @Override
    public List<BusinessCenterMessageEntity> getBMMessageMenu(Set<Integer> b_m_idS) {
        List<BusinessCenterMessageEntity> businessCenterMessageEntities = new ArrayList<>();
        for (Integer b_m_id : b_m_idS) {
            QueryWrapper<BusinessCenterMessageEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("bm_id",b_m_id);
            BusinessCenterMessageEntity businessCenterMessageEntity = businessCenterMessageMapper.selectOne(queryWrapper);
            if (businessCenterMessageEntity != null) {
                businessCenterMessageEntities.add(businessCenterMessageEntity);
            }

        }
        return businessCenterMessageEntities;
    }

    @Override
    public List<MongoDB_S_C_SaveSCDailyContextEntity> findS_C_ExMessage(Permission_TBEntity permission_tbEntity) {

        return mongoDB_s_c_saveDailyContextDao.findS_C_ExMessage(permission_tbEntity);
    }

    @Override
    public List<MessageCenterEntity> midFindMessageCenterSidBar(Set<Integer> midS) {
        List<MessageCenterEntity> messageCenterEntities = new ArrayList<>();
        for (Integer mid : midS) {
            QueryWrapper<MessageCenterEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", mid);
            MessageCenterEntity messageCenterEntity = messageCenterMapper.selectOne(queryWrapper);
            if (messageCenterEntity != null) {
                messageCenterEntities.add(messageCenterEntity);
            }

        }
        return messageCenterEntities;
    }

    @Override
    public List<BusinessCenterMessageEntity_per> perIdFindBMMessageMid(Permission_TBEntity permission_tbEntity) {
        QueryWrapper<BusinessCenterMessageEntity_per> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("per",permission_tbEntity.getPermission());
        return businessCenterMessage_perMapper.selectList(queryWrapper);
    }

}
